package self.hoy.demo.rabbitmq.topic;

import com.rabbitmq.client.*;
import self.hoy.demo.rabbitmq.Constant;
import self.hoy.demo.rabbitmq.util.ConnectionUtils;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author hyh
 * @date 2020/6/7
 */
public class NewsReceive implements Constant {
    private static final String QUEUE_NAME = "test_queue_topic_1";
    private static final String EXCHANGE_NAME = "test_exchange_topic_1";
    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection = ConnectionUtils.getConnection();
        Channel channel = connection.createChannel();
        channel.exchangeDeclare(EXCHANGE_NAME, TYPE_TOPIC);
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        final String routeKey = "#.add";
//        final String routeKey = "news.#";
        channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, routeKey);
        DefaultConsumer consumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println(routeKey + " topic test receiver message : " + new String(body));
            }
        };
        channel.basicConsume(QUEUE_NAME, true, consumer);
    }
}
